// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var Global = require("Global");

cc.Class({
    extends: cc.Component,

    properties: {
        // 这个属性引用了星星预制资源
        starPrefab: {
            default: null,
            type: cc.Prefab
        },
        // 星星产生后消失时间的随机范围
        maxStarDuration: 0,
        minStarDuration: 0,
        // 地面节点，用于确定星星生成的高度
        ground: {
            default: null,
            type: cc.Node
        },
        // player 节点，用于获取主角弹跳的高度，和控制主角行动开关
        player: {
            default: null,
            type: cc.Node
        },
        scoreDisplay: {
            default: null,
            type: cc.Label
        },
        startButton: {
            default: null,
            type: cc.Node
        },
        sprite: {
            default: null,
            type: cc.SpriteFrame,
        },
        testNode: {
            default: null,
            type: cc.Node
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        // 获取地平面的 y 轴坐标
        this.groundY = this.ground.y + this.ground.height/2;
        // 生成一个新的星星
        this.spawnNewStar();
        // 初始化计分
        this.score = 0;

        Global.backNode = this.startButton;
        Global.backLabel = this.getComponent(cc.Label);

        // Test
        this._testCrreateNode();
    },

    start: function () {
        
    },

    update: function (dt) {
        if (cc.isValid(this.testNode)) {
            this.testNode.rotation += dt * 40.0;
        }
    },

    onDestroy: function () {

    },

    onEnable: function() {

    },

    onDisable: function() {

    },

    // Private Functions:

    spawnNewStar: function() {
        // 使用给定的模板在场景中生成一个新节点
        var newStar = cc.instantiate(this.starPrefab);
        // 将新增的节点添加到 Canvas 节点下面
        this.node.addChild(newStar);
        // 为星星设置一个随机位置
        newStar.setPosition(this.getNewStarPosition());

        // 将 Game 组件的实例传入星星组件
        newStar.getComponent('Star').game = this;
    },

    getNewStarPosition: function () {
        var randX = 0;
        // 根据地平面位置和主角跳跃高度，随机得到一个星星的 y 坐标
        var randY = this.groundY + cc.random0To1() * this.player.getComponent('Player').jumpHeight + 50;
        // 根据屏幕宽度，随机得到一个星星 x 坐标
        var maxX = this.node.width/2;
        randX = cc.randomMinus1To1() * maxX;
        // 返回星星坐标
        return cc.p(randX, randY);
    },


    gainScore: function () {
        this.score += 1;
        // 更新 scoreDisplay Label 的文字
        this.scoreDisplay.string = 'Score: ' + this.score.toString();
    },


    _testClassDefined: function() {
        console.log("定义 CCClass");
        // 定义 CCClass
        var Sprite = cc.Class({
            name: "sprite",
            ctor: function() {
                console.log("ctor invoked");
                cc.log(this instanceof Sprite); // true
            },
            print: function() {

            }
        });
        var obj = new Sprite();

        // 继承
        var Shape = cc.Class({
            ctor: function () {
                cc.log("Shape");    // 实例化时，父构造函数会自动调用，
            },
            properties: {
                userID: 20,
                userName: "Foobar"
            }
        });
        var Rect = cc.Class({
            extends: Shape
        });

        var Square = cc.Class({
            extends: Rect,
            ctor: function () {
                cc.log("Square");   // 再调用子构造函数
            }
        });

        var square = new Square();
    },

    _testCrreateNode() {
        // 克隆已有节点
        var scene = cc.director.getScene();
        var node = cc.instantiate(this.player);
        node.parent = scene;
        node.setPosition(0, 0);
        node.scaleX = 2;
        node.scaleY = 1.5;
        // node.node.color = cc.Color.GREEN;
        
        this.startButton.setPosition(200, 0);

        var node2 = new cc.Node('Sprite');
        var sp = node2.addComponent(cc.Sprite);
        sp.spriteFrame = this.sprite;
        node2.parent = this.node;
        this.testNode = node2;

        setTimeout(function () {
            if(this.testNode) {
                this.testNode.destroy();
            }
            console.log("=======time out");
          }.bind(this), 5000);
    },

    onStartButtonClick: function() {
        console.log("onStartButtonClick");
        cc.director.loadScene("MyScene");
    },
});
